<template>
  <div>
    <i class="el-input__icon" :class="'el-icon-caret-' + icon" @click="handleToggle"></i>
    <input readonly :value="currentValue" @input="handleInput" @click="handleToggle">
    <div class="el_tree_box" v-show="elTreeShow">
      <el-tree :data="treeData" :props="defaultProps" default-expand-all @node-click="handleNodeClick"></el-tree>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      treeData: {
        type: Array
      },
      value: String
    },
    data () {
      return {
        defaultProps: {
          children: "children",
          label: "name"
        },
        elTreeShow: false,
        currentValue: this.value,
        icon: "bottom"
      }
    },
    watch: {
      value(val) {
        this.currentValue = val;
      }
    },
    methods: {
      handleNodeClick (data) {
        this.currentValue = data.name;
        this.$emit("input", data.name);
        this.handleToggle();
      },
      handleInput (event) {
        const value = event.target.value;
        this.$emit("input", value);
      },
      handleToggle () {
        this.elTreeShow = !this.elTreeShow;
        this.icon = this.icon === "top" ? "bottom" : "top";
      }
    }
  }
</script>

<style scoped lang="scss">
  input {
    width: 100%;
    height: 50px;
    line-height: 1;
    padding: 0 10px;
    border: 1px solid #bfcbd9;
    border-radius: 3px;
    box-sizing: border-box;
    color: #1f2d3d;
    outline: none;
    font-size: 16px;
    cursor: pointer;
  }

  .el-input__icon {
    cursor: pointer;
  }

  .el-input__icon:after {
    content: '';
    height: 100%;
    width: 0;
    display: inline-block;
    vertical-align: middle;
  }

  .el_tree_box {
    width: 100%;
    height: 200px;
    overflow-y: scroll;
    position: absolute;
    z-index: 10000;
    border: 1px solid #d1dbe5;
    box-sizing: border-box;
    background-color: #fff;

    .el-tree {
      border: none;
    }

    .el-tree-node__content {
      height: 24px;
      line-height: 24px;
    }

    .el-tree-node__label {
      font-size: 14px;
    }
  }
</style>
